import 'package:flutter/material.dart';

class ListViewExps extends StatefulWidget {
  const ListViewExps({super.key});

  @override
  State<ListViewExps> createState() => _ListViewExpsState();
}

class _ListViewExpsState extends State<ListViewExps> {
  int _currentIndex = 0;

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: SafeArea(
          child: Center(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: [
            Row(
              children: [
                _buildTabButton(0, '普通列表'),
                _buildTabButton(1, '动态列表'),
                _buildTabButton(2, '分隔列表'),
              ],
            ),
            Expanded(
              child: _buildListView(_currentIndex),
            )
          ],
        ),
      )),
    );
  }

  Widget _buildTabButton(int index, String title) {
    return TextButton(
      onPressed: () {
        setState(() {
          _currentIndex = index;
        });
      },
      child: Text(title),
    );
  }

  Widget _buildListView(int index) {
    if (index == 0) {
      return MyListView();
    } else if (index == 1) {
      return DynamicListView();
    } else {
      return SeparatedListView();
    }
  }
}

class MyListView extends StatelessWidget {
  const MyListView({super.key});

  @override
  Widget build(BuildContext context) {
    return ListView(
      shrinkWrap: true,
      children: [
        ListTile(
          leading: Icon(Icons.person),
          title: Text('张三'),
          subtitle: Text('前端开发工程师'),
        ),
        ListTile(
          leading: Icon(Icons.person),
          title: Text('李四'),
          subtitle: Text('后端开发工程师'),
        ),
        ListTile(
          leading: Icon(Icons.person),
          title: Text('王五'),
          subtitle: Text('UI/UX 设计师'),
        ),
        ListTile(
          leading: Icon(Icons.person),
          title: Text('赵六'),
          subtitle: Text('产品经理'),
        ),
        ListTile(
          leading: Icon(Icons.person),
          title: Text('孙七'),
          subtitle: Text('测试工程师'),
        ),
      ],
    );
  }
}

class DynamicListView extends StatelessWidget {
  // 模拟一些数据
  final List<String> items = List.generate(20, (index) => '列表项 ${index + 1}');

  DynamicListView({super.key});

  @override
  Widget build(BuildContext context) {
    return ListView.builder(
      shrinkWrap: true,
      itemCount: items.length,
      itemBuilder: (context, index) {
        return ListTile(
          leading: Icon(Icons.article),
          title: Text(items[index]),
        );
      },
    );
  }
}

class SeparatedListView extends StatelessWidget {
  final List<String> items = List.generate(10, (i) => '项目 ${i + 1}');

  SeparatedListView({super.key});

  @override
  Widget build(BuildContext context) {
    return ListView.separated(
      itemCount: items.length,
      shrinkWrap: true,
      separatorBuilder: (context, index) => Divider(color: Colors.grey),
      itemBuilder: (context, index) {
        return ListTile(
          leading: Icon(Icons.star),
          title: Text(items[index]),
        );
      },
    );
  }
}
